/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package marsdonne;

import marsdonne.tasks.ForwardingTask;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.rabbitmq.RMQSource;
import org.apache.flink.streaming.connectors.rabbitmq.common.RMQConnectionConfig;

import java.sql.Types;

/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class StreamingJob {

    public static final String insertSQL = "insert into data_business(type,timed,metadata,contents) values(?,?,?::jsonb,?::jsonb) on conflict(type,timed) do UPDATE set metadata = data_business.metadata, contents = data_business.contents";

    public static void main(String[] args) throws Exception {
        final RMQConnectionConfig connectionConfig = new RMQConnectionConfig.Builder()
                .setUserName("guest")
                .setPassword("guest")
                .setVirtualHost("/")//multi-tenant
                .setHost("192.168.0.125")
                .setPort(5672)
                .build();

        final RMQSource<String> rmqSource = new RMQSource<>(
                connectionConfig,            // config for the RabbitMQ connection
                "data.business.increment.forwarding",                   // name of the RabbitMQ queue to consume
                false,      // use correlation ids; can be false if only at-least-once is required
                new SimpleStringSchema());

        // set up the streaming execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // checkpointing is required for exactly-once or at-least-once guarantees
        env.enableCheckpointing(60 * 1000);//单位毫秒，意外终止程序会从最近一个检查点回滚数据和状态

        new ForwardingTask(
                ForwardingTask.DbType.PgSql,
                insertSQL,
                new int[]{Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR}
        ).submitTo(env, rmqSource);
    }
}
